package com.umeox.watch.moto.alarm.ui;


import static com.umeox.watch.moto.dataservice.constants.Constants.ACTION_UPDATED_ALARMLIST;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.dialog.ActionOptionsDialog;
import com.umeox.moto.watch.themes.widget.TextView;
import com.umeox.watch.moto.alarm.AlarmDetail;
import com.umeox.watch.moto.alarm.R;
import com.umeox.watch.moto.alarm.Utils;
import com.umeox.watch.moto.alarm.adapter.AlarmAdapter;
import com.umeox.watch.moto.alarm.base.Constants;

import java.util.List;


public class AlarmClockListActivity extends ThemeAppCompatActivity implements AlarmAdapter.OnItemClickListener, View.OnClickListener {
    private static final String TAG = AlarmClockListActivity.class.getSimpleName();
    public static final int[] ICONS = {R.drawable.alarm_clock_edit, R.drawable.alarm_clock_delete};
    public static final int[] DESTS = {R.string.edit, R.string.delete};

    private AlarmAdapter alarmAdapter;
    private DataReceiver receiver;

    private TextView mAlarmEmpty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        initView();
        registerReceiver();
    }


    private void initView() {
        RecyclerView contactList = findViewById(R.id.clockList);
        mAlarmEmpty = findViewById(R.id.tv_alarm_empty);
        findViewById(R.id.iv_add).setOnClickListener(this);

        contactList.setLayoutManager(new LinearLayoutManager(this));
        final List<AlarmDetail> alarmList = Utils.getAlarmList();
        alarmAdapter = new AlarmAdapter(this, alarmList);
        alarmAdapter.setOnClickListener(this);
        contactList.setAdapter(alarmAdapter);
        mAlarmEmpty.setVisibility(alarmList.isEmpty() ? View.VISIBLE : View.GONE);
    }

    public boolean canScrollToVertical(RecyclerView recyclerView, int direction) {

        final int range = recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollExtent();

        final int offset = recyclerView.computeVerticalScrollOffset();
        Log.d(TAG, "canScrollToVertical range: " + range + " offset:" + offset);
        if (range == 0) return false;

        if (direction < 0) {
            return offset >= 240;
        }
        return offset < 480;
    }

    private void registerReceiver() {
        receiver = new DataReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_UPDATED_ALARMLIST);
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    @Override
    public void onItemClick(AlarmDetail alarmDetail) {
        if (alarmDetail == null) {
            onClick(null);
            return;
        }
        ActionOptionsDialog optionsDialog = new ActionOptionsDialog(this, DESTS, ICONS, this);
        final ActionOptionsDialog.OnClickListener onClickListener = new ActionOptionsDialog.OnClickListener() {
            @Override
            public void onOption1Click() {
                final Intent intent = new Intent(AlarmClockListActivity.this, SetAlarmActivity.class);
                intent.putExtra(Constants.EXTRA_KEY_ALARM, alarmDetail);
                startActivity(intent);
            }

            @Override
            public void onOption2Click() {
                final Intent intent = new Intent(AlarmClockListActivity.this, ConfirmDeleteActivity.class);
                intent.putExtra(Constants.EXTRA_KEY_ALARM, alarmDetail);
                startActivity(intent);
            }

            @Override
            public void onOption3Click() {

            }

            @Override
            public void onOption4Click() {

            }
        };
        optionsDialog.setOnClickListener(onClickListener);
        optionsDialog.show();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.iv_add){
            startActivity(new Intent(AlarmClockListActivity.this, SetAlarmActivity.class));
        }
    }


    class DataReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_UPDATED_ALARMLIST.equals(action)) {

                List<AlarmDetail> list = Utils.getAlarmList();
                Logger.d(TAG, "闹钟列表更新，刷新页面 " + list.size());
                final boolean empty = list.isEmpty();
                mAlarmEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
                alarmAdapter.setDatas(list);
                alarmAdapter.notifyDataSetChanged();
            }
        }
    }
}
